const Com = require("../gmoon/baseCom");
const virusScript = require("../elms/virusn");
const STATE = {
    NOLOAD : 1,
    LOADING : 2,
    LOADED : 3,
    UNLOADING : 4,
    UNLOADED : 5
}
cc.Class({
    extends: Com,

    properties: {
    },

    ctor(){
        this.__virus = [];
    },

    onLoad(){
        this.setState(STATE.NOLOAD);
    },

    load(){
        return new Promise(( res )=>{
            this.useResource()
            .then(( d )=>{
                this.node.active = true;
                this.setAttr("_resLoaded" , true);
                this.resetData();
                this.playBianfu();
                this.resetData().loadVirus();

                this.setState(STATE.LOADED);
                res();
            })
        });
    },

    unload(){
        this.setState(STATE.UNLOADING);
        return new Promise(( res )=>{
            this.setState(STATE.UNLOADED);
            this.node.active = false;
            res();
        });
    },

    useResource(){
        this.setState(STATE.LOADING);
        return $RES.use(
            "fab-virus #病毒组件",
            "fab-anode",
            "img-tx-vboom1",
            "img-tx-vboom2",
            "img-tx-vbooma",
            "img-tx-vbooma1",
            "img-vboom-boom1",
            "img-vboom-boom2",
            "img-vboom-boomB1",

            "img-boss-bf1",
            "img-boss-bf2" ,

            "imgs-boom-blue",
            "imgs-boom-green",
            "imgs-boom-green1",
            "imgs-boom-yellow",
            "imgs-boom-yellow1",
            "imgs-boom-zise",

            "imgs-jinbi-frm"
        );
    },

    playBianfu(){
        if(this.__bfNode) return;
        this.__bfNode = $G.get('poolGetByName' , 'anode') || $RES.createPrefab("fab-anode");
        this.__bfNode.parent = this.node;
        this.__bfNode.setPosition(0 , 250);
        this.__bfNode.width = 500;

        
        $T.getScr(this.__bfNode).playFreameAnimate([
            {
                frame : $RES.g("img-boss-bf1"),
                size : [290 , 301],
                opacity : 70,//[ 50, 80] , 
                time : .3,
                param : {},
                //rotation: [0 , $T.random(100,180) ],
                callfn : ()=>{
                }
            },
            {
                frame : $RES.g("img-boss-bf2"),
                size : [290 , 301],
                opacity : 100,//[ 50, 80] , 
                time : .1,
                param : { },
                //rotation: [0 , $T.random(100,180) ],
                callfn : ()=>{
                }
            }
        ] , true ).then(()=>{
        });
    },

    loadVirus( num , pos ){
        this.createVirus( num  , pos );
    },

    createVirus( num , pos ){
        num = num || 1;
        let pool = $G.get("pool-virus");
        let virus , viruses = $CONF.get("virus") , virusArr=[];
        for(let k in viruses){
            virusArr.push( viruses[k] );
        }
        // console.log( "num: %d" , this.__virus.length )
        for(let i=0 ; i<num ; i++){
            virus = pool.get() || $RES.createPrefab("fab-virus");
            virus.active = true;
            virus.parent = this.node;

            let vscr = $T.getScr(virus);
            vscr.setBaseInfo({
                base : {
                    refer : this.node ,
                    toW : 1 , 
                    toH : 1 ,
                    stW : .5,
                    stH : .5,
                    stO : 160
                },
                vinf : $CONF.do("virus" , "getRandom") //virusArr[$T.random(0,virusArr.length-1)]
            })
            .reuse( (new Date()).valueOf() )
            .run( pos );

            this.__virus.push(virus);
        }
    },

    removeVirus( vid ){
        for(let i in this.__virus){
            let tid = $T.getScr(this.__virus[i]).getAttr("_vid");
            if(tid == vid){
                this.__virus.splice( i , 1);
                return;
            }
        }
    },

    resetData(){
        let pool = $G.get("pool-virus");
        for(let i in this.__virus){
            this.__virus[i].active = false;
            pool.put(this.__virus[i]);
        }
        this.__virus = [];
        return this;
    },

    get( num , type ){
        num = num || 1;

        let re = [];
        let viruses = this.children("virusn"),
            tvs=[] , ti;
        if(type == "most"){
            let v , tv , f , j=0;
            for(let i in viruses){
                f = false , j=0 , v = $T.getScr( viruses[i] );
                while( 1 ){
                    if(!tvs[j]){
                        tvs[j] = viruses[i];
                        break;
                    }else{
                        tv = $T.getScr( tvs[j] );
                        // if( v.getAttr("yr") >= tv.getAttr("yr") ){
                        //     tvs.splice(j,0,viruses[i])
                        //     break;
                        // }
                        if( v.node.y <= tv.node.y ){
                            tvs.splice(j,0,viruses[i])
                            break;
                        }
                    }
                    j++;
                }
            }
            viruses = [];
            for(let i=0; i<2 && i<tvs.length; i++){
                viruses.push( tvs[i] );
            }
        }
        
        if( $T.isType(num , "Number") ){
            for(let i=0; i<num; i++){
                if(viruses.length<1) break;
                ti = $T.random(1,viruses.length)-1;
                re.push( viruses[ti] );
                viruses.splice(ti , 1)
            }
            return num==1 ? re[0] : re;
        }else if( num == "allpos"){
            re = Object.assign([], viruses);
        }

        return re;
    },

    update( dt ){
        if( !this.getAttr("_resLoaded") ) return;
        this._zhen = this._zhen || this.childNode("circle > circleZhen");
        //时间累加器
        let acc = this.getAttr("acc");
        this.setAttr("acc" , ( acc ? acc : 0 ) + dt );
        // for(let i=0; i<this.__virus.length; i++){
        //     if( this.__virus[i] && !this.__virus[i].active ){
        //         this.__virus.splice( i , 1);
        //         i--;
        //     }
        // }

        let num = this.__virus.length;
        if( this.getAttr("acc")>= 1.6 && this.__virus.length<50 ){
            this.createVirus(1);
            this.setAttr("acc" , 0 );
        }

        if( this._zhen ){
            let c = this._zhen.rotation;
            c = c + dt *20;
            c = c >=360 ? (c-360) : c;
            this._zhen.rotation = c;
        }
    }
});
